package com.china.zhuoyue.ui.activity.estate

import android.view.View
import androidx.activity.viewModels
import androidx.recyclerview.widget.DividerItemDecoration
import com.amap.api.services.core.LatLonPoint
import com.amap.api.services.core.PoiItem
import com.amap.api.services.geocoder.GeocodeSearch
import com.amap.api.services.geocoder.RegeocodeQuery
import com.amap.api.services.poisearch.PoiSearch
import com.china.zhuoyue.R
import com.china.zhuoyue.api.login.launchUILogin
import com.china.zhuoyue.bean.IndexFun
import com.china.zhuoyue.databinding.ActivitySmartStopCarBinding
import com.china.zhuoyue.databinding.ItemNearbyParkingLotBinding
import com.china.zhuoyue.map.viewmodel.MapViewModel
import com.china.zhuoyue.ui.api.ApiObserver
import com.china.zhuoyue.ui.base.BaseActivity
import com.china.zhuoyue.ui.base.BaseRecyclerAdapter
import com.china.zhuoyue.ui.base.BaseViewHolder
import com.china.zhuoyue.ui.fragment.IndexFunAdapter
import com.china.zhuoyue.ui.helper.PopupNav
import com.china.zhuoyue.ui.utils.FullyGridLayoutManager
import com.china.zhuoyue.ui.utils.FullyLinearLayoutManager
import com.china.zhuoyue.ui.utils.GlideImageLoader
import com.youth.banner.BannerConfig
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

class SmartStopCarActivity : BaseActivity<ActivitySmartStopCarBinding>() {
    private val mMapViewModel: MapViewModel by viewModels()
    private val mNearbyParkingLotAdapter = NearbyParkingLotAdapter()
    private var mPoiItem: PoiItem? = null

    override fun initView() {
        loadView {
            bindViewClick(buttonNav)
            val cover = mutableListOf<Int>().apply {
                add(R.mipmap.banner_smart_stop_car_1)
                add(R.mipmap.banner_smart_stop_car_2)
                add(R.mipmap.banner_smart_stop_car_3)
            }
            banner.run {
                setImageLoader(GlideImageLoader())
                setIndicatorGravity(BannerConfig.RIGHT)
                setBannerStyle(BannerConfig.CIRCLE_INDICATOR)
                setDelayTime(2000)
                setImages(cover)
            }.start()
            rvFun.adapter = IndexFunAdapter().apply {
                datas = mutableListOf<IndexFun>().apply {
                    add(IndexFun(R.string.text_smart_stop_car_fun_1, R.mipmap.icon_smart_stop_car))
                    add(IndexFun(R.string.text_smart_stop_car_fun_2, R.mipmap.icon_smart_stop_car_pay))
                    add(IndexFun(R.string.text_smart_stop_car_fun_3, R.mipmap.icon_smart_stop_car_monthly_card))
                    add(IndexFun(R.string.text_smart_stop_car_fun_4, R.mipmap.icon_smart_stop_car_record))
                }
            }
            rvFun.layoutManager = FullyGridLayoutManager(mThis, 4)

            rvParkingLot.adapter = mNearbyParkingLotAdapter
            rvParkingLot.addItemDecoration(DividerItemDecoration(mThis, DividerItemDecoration.VERTICAL))
            rvParkingLot.layoutManager = FullyLinearLayoutManager(mThis)
        }
    }

    override fun onClick(v: View?) {
        when (v) {
            vb.buttonNav -> {
                mPoiItem?.apply {
                    PopupNav.goNav(v.context, latLonPoint.latitude, latLonPoint.longitude, snippet?.ifBlank { title } ?: "")
                }
            }
        }
    }

    override fun initData() {
        mMapViewModel.mLocationChange.observe(this) {
            it?.let {
                launchUILogin({
                    val pois = withContext(Dispatchers.IO) {

                        val geocoderSearch = GeocodeSearch(mThis)
                        val regeocodeQuery = RegeocodeQuery(LatLonPoint(it.latitude, it.longitude), 1000f, GeocodeSearch.AMAP)
                        val regeocodeAddress = geocoderSearch.getFromLocation(regeocodeQuery)

                        val poiSearchQuery = PoiSearch.Query(null, "150904|150905|150906", regeocodeAddress.cityCode)
                        poiSearchQuery.pageSize = 10
                        poiSearchQuery.pageNum = 1

                        val poiSearch = PoiSearch(mThis, poiSearchQuery)
                        poiSearch.bound = PoiSearch.SearchBound(LatLonPoint(it.latitude, it.longitude), 2000)
                        poiSearch.searchPOI()?.pois
                    }
                    pois
                }, ApiObserver {
                    if (it?.isNullOrEmpty() == false) {
                        it[0]?.apply {
                            mPoiItem = this
                            vb.tvStopCarRecommendAddress.text = title
                            vb.tvStopCarRecommendDistance.text = "<${distance}米"
                        }
                        mNearbyParkingLotAdapter.datas = it.filterIndexed { index: Int, poiItem: PoiItem? -> index > 0 }.toMutableList()
                        mNearbyParkingLotAdapter.notifyDataSetChanged()
                    }
                })
            }
        }

    }
}

class NearbyParkingLotAdapter : BaseRecyclerAdapter<BaseViewHolder<ItemNearbyParkingLotBinding>, PoiItem>() {

    override fun onBindViewHolder(holder: BaseViewHolder<ItemNearbyParkingLotBinding>, bean: PoiItem, position: Int) {
        holder.vb.apply {
            bean.apply {
                tvStopCarRecommendTitle.text = title
                tvStopCarRecommendAddress.text = "地址：${snippet?.ifBlank { title }}"
                tvStopCarRecommendDistance.text = "${distance}米"
            }
            setViewClick(position, buttonNav)
        }
    }

    override fun subOnClickAdapter(v: View, position: Int) {
        getItem(position).apply {
            PopupNav.goNav(v.context, latLonPoint.latitude, latLonPoint.longitude, snippet?.ifBlank { title } ?: "")
        }
    }
}